from import_export import resources


class ModelResource(resources.ModelResource):
    def __init__(self):
        super(ModelResource, self).__init__()
        field_list = self._meta.model._meta.fields
        # 应用名与模型名
        self.verbose_name_dict = {}
        # 获取所有字段的verbose_name并存放在verbose_name_dict字典里
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        # 默认导入导出field的column_name为字段的名称
        fields = self.get_fields()
        # print(fields)

        # 这里修改为字段的verbose_name
        for field in fields:
            field_name = self.get_field_name(field)
            # 如果设置过verbose_name，则将column_name替换为verbose_name
            # 否则维持原有的字段名
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]

        return fields
